import apiClient, { wrapApiCall } from './api';
import type {
  ConnectionDetail,
  CreateConnectionRequest,
  ListResponse,
  ApiResponse
} from '../types/api';

// 连接管理API服务
export const connectionService = {
  // 获取所有连接, 使用ListResponse包装响应
  listConnections: async (): Promise<ConnectionDetail[]> => {
    const response = await wrapApiCall<ListResponse<ConnectionDetail>>(apiClient.get('/connections'));
    
    if (!response.connections) {
      throw new Error('连接列表响应数据为空');
    }
    
    return response.connections;
  },


  // 获取连接详情
  getConnection: async (connectionId: string): Promise<ConnectionDetail> => {
    const response = await wrapApiCall<ConnectionDetail>(apiClient.get('/connection', {
      params: { connection: connectionId }
    }));
    
    return response;
  },

  // 创建新连接
  createConnection: async (data: CreateConnectionRequest): Promise<ConnectionDetail> => {
    const response = await wrapApiCall<ApiResponse>(apiClient.post('/connections', data));
    
    if (!response.data) {
      throw new Error('创建连接响应数据为空');
    }
    
    return response.data as ConnectionDetail;
  },

  // 删除连接
  deleteConnection: async (connectionId: string): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.delete('/connection', {
      params: { connection: connectionId }
    }));
  },

  // 设置默认连接
  setDefaultConnection: async (connectionId: string): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.post('/connection/set-default', null, {
      params: { connection: connectionId }
    }));
  },

  // 关闭连接
  closeConnection: async (connectionId: string): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.post('/connection/close', null, {
      params: { connection: connectionId }
    }));
  },

  // 重新连接
  reconnectConnection: async (connectionId: string): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.post('/connection/reconnect', null, {
      params: { connection: connectionId }
    }));
  },

  // 重新连接所有连接
  reconnectAllConnections: async (): Promise<void> => {
    await wrapApiCall<ApiResponse>(apiClient.post('/connections/reconnect-all'));
  },

  // 健康检查
  checkConnectionHealth: async (connectionId: string): Promise<{ status: string; message: string }> => {
    const response = await wrapApiCall<{ status: string; message: string }>(apiClient.get('/connection/health', {
      params: { connection: connectionId }
    }));
    
    return response;
  },

  // 获取连接信息
  getConnectionInfo: async (connectionId: string): Promise<any> => {
    const response = await wrapApiCall<any>(apiClient.get('/connection/info', {
      params: { connection: connectionId }
    }));
    
    return response;
  },

  // 获取连接能力
  getConnectionCapabilities: async (connectionId: string): Promise<string> => {
    const response = await apiClient.get('/connection/capabilities', {
      params: { connection: connectionId },
      responseType: 'text',
    });
    return response.data;
  },
};

export default connectionService;